import pyaudio
import unittest
import numpy as np


def getDevices() -> list:
    pa = pyaudio.PyAudio()
    devlist = list()
    defaultdev = pa.get_default_input_device_info()
    if defaultdev['maxInputChannels']:
        devlist.append(defaultdev)
    for devindex in range(pa.get_device_count()):
        dev = pa.get_device_info_by_index(devindex)
        if dev['maxInputChannels'] and dev not in devlist:
            devlist.append(dev)
    return devlist


class Test(unittest.TestCase):

    def test_1(self):
        print(getDevices())

    def test_2(self):
        import time
        pa = pyaudio.PyAudio()
        stream = pa.open(format=pyaudio.paInt8,
                         channels=1,
                         rate=8000,
                         input=True,
                         frames_per_buffer=1024,
                         stream_callback=callback)
        stream.start_stream()
        while stream.is_active():
            time.sleep(0.001)
        print("录制结束")
